# -*- encoding=utf8 -*-
__author__ = "10511.ZNPT.000"

import random
from airtest.core.api import *
import time
import tkinter as tk
import numpy as np
# from outdate_function import start_GuoZhan,start_YanWu
from time import strftime, gmtime
auto_setup(__file__)
from airtest.core.settings import Settings as ST
from airtest.aircv import *
ST.CVSTRATEGY = ["tpl", "sift"]
ST.FIND_TIMEOUT_TMP = 2
ST.SAVE_IMAGE = False
# connect_device("android://127.0.0.1:5037/emulator-5560")
# --------------------------------------------------------------------------------
# --------------------------------------------------------------------------------
# 目前的功能有：
#   0、自动换号登录   
#   1、签到
#   2、领取每日免费礼包，月卡周卡，元宝树，周薪(先锋以上)，邮件
#   3、刷历练值，5人军争，无需点将
#   4、领取每日任务奖励，以及活跃礼包
#   5、领取激活码
#   6、领取节钺令、天子令
#   7.1、征战天下日活（仅包括扫荡、领取每日俸禄、活跃值，不会去打每日任务，升星，购物等）
#   7.2、自动征战天下演武，不保证输赢情况（最多尝试2次，只打9场），请先自行配置好出战阵容
#   8、领取家园奖励，拜访好友（请确保事先完成新手引导）
# --------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------
# 自动切换账号登录，目前只支持官服
# 只有开启自动换号登录才需要填写账号密码，请保证开启功能后，界面位于开始游戏界面，
# 且一定要保证页面上面出现了《更换账号》的提示，否则请重新进入开始界面
# 如若不开启自动切换账号功能，请将界面停留在进入游戏后的页面（就是经典场、排位场、活动场那个界面）
# 账号按照如下格式自行修改补充
users = ['user1','user2']
pwds = ['pwd1','pwd2']
# 激活码填入codes中,书写方式与上述相同，可以允许多个
codes = ['感恩节送好礼','感恩节派对']
# 演练次数，默认-1表示直到历练值满为止；改为大于0的数字表示演练轮数，（4次军争+1次标准=5）为一轮。
epoch = -1
###########################################################

###########################################################
host = [''] # 作者测试至尊1v1添加，无需理会
host_pwd = ['2222'] # 作者测试至尊1v1添加，无需理会
host_stat = 0 # 默认为0，不要修改
# 功能开关，True为开启，False为关闭
sw={"0 自动换号登录（官服）":True,
    "1 签到":True,
    "2.1 领取每日免费盒子":True,
    "2.2-2.3 领取月卡周卡，元宝树":True,
    "2.4 领取周薪":True,
    "2.5 领取邮件":True,
    "3 开始历练":True,
    "4 领取日活、周活":True,
    "5 领取激活码":True,
    "6 领取节钺令、天子令":True,
    "7.1 征战天下日活":True,
    "7.2 征战天下自动演武":True,
    "8 领取家园（请确保事先完成新手引导）":True,
    "9 寻味三国任务奖励领取": True,
    "10 寻味三国自动兑换一盒一福袋": True,
    "11 领取琳琅奇境": True,
    "12 闲趣百市": True,
    }
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
def match_snapshot(tempalte,x1=0,y1=0,x2=1600,y2=900,screen=None):
    
    if screen is None:
        screen = G.DEVICE.snapshot()
    # 在截图里面查找指定的图片对象
    pos = tempalte.match_in(screen)
    # 返回找到的图片对象的坐标（该坐标是相对于局部截图的坐标）
    if not pos:
        return None
    # 若要返回目标在整个屏幕中的坐标，则x，y都需要加上局部截图时设置的最小x、y
    else:
        return (pos[0]+x1,pos[1]+y1)
    
def cal_confidence(tempalte,x1=0,y1=0,x2=1600,y2=900,screen=None):
    
    if screen is None:
        screen = G.DEVICE.snapshot()
    # 在截图里面查找指定的图片对象
    match_result = tempalte._cv_match(screen)
    # 返回找到的图片对象的坐标（该坐标是相对于局部截图的坐标）
    if not match_result:
        return None
    # 若要返回目标在整个屏幕中的坐标，则x，y都需要加上局部截图时设置的最小x、y
    else:
        return match_result.get('confidence')
    
def find_max_index_numpy(lst):
    # 将列表转换为NumPy数组，None在数组中会表现为np.nan（对于浮点数）或需要指定dtype=object
    arr = np.array(lst, dtype=object)  # 使用dtype=object来混合保存不同类型数据
    
    # 创建一个布尔掩码，标记哪些位置不是None
    mask = np.array([x is not None for x in lst])
    
    if not mask.any():  # 检查是否存在有效值（掩码中是否有True）
        return 5  # 如果没有有效值，返回None
    
    # 对有效部分（由掩码确定）应用argmax，但需要拿到在原始列表中的索引
    # 我们可以创建一个索引数组，然后取掩码后有效部分中最大值的"局部"索引，再映射回原索引
    valid_indices = np.where(mask)[0]  # 所有非None值的原始下标
    valid_values = arr[mask]  # 所有非None的值
    
    if len(valid_values) == 0:
        return 5
        
    # 在有效值中找最大值的"局部"索引
    local_max_index = np.argmax(valid_values)
    # 将"局部"索引映射回原始列表的索引
    global_max_index = valid_indices[local_max_index]
    
    return global_max_index
def touch_back():
    try_times = 0
    while try_times <2:
        try_times += 1
        pos = match_snapshot(Template(r"tpl1738736529990.png", threshold=0.8, record_pos=(0.467, -0.244), resolution=(1600, 900)))
        if pos:
            try_times = 0
            touch(pos) # 返回键的位置
            sleep(0.3)

def touch_add_button():
    try_times = 0
    while try_times <2:
        try_times += 1
        pos = match_snapshot(Template(r"tpl1757214342760.png", threshold=0.85, record_pos=(0.464, 0.237), resolution=(1600, 900)))
        if pos:
            try_times = 0
            touch(pos) # 返回键的位置
            sleep(0.3)
   #   0、自动换号登录
def login_guanFu(i):
    if match_snapshot(Template(r"tpl1734414961671.png", threshold=0.85, record_pos=(-0.002, -0.235), resolution=(1600, 900))):
        touch((45,50))#点击空白去除狗卡公告
        sleep(0.5)
    if match_snapshot(Template(r"tpl1734193454891.png", record_pos=(0.082, -0.255), resolution=(1600, 900))):
        touch((934,48))#点击更换
        sleep(0.5)
    elif not match_snapshot(Template(r"tpl1734245415373.png", threshold=0.8, record_pos=(0.006, 0.013), resolution=(1600, 900))):
        touch((1300,555))
        sleep(0.5)
    if match_snapshot(Template(r"tpl1734245415373.png", threshold=0.8, record_pos=(0.006, 0.013), resolution=(1600, 900))):
        sleep(1)
        touch((797,241))
        sleep(0.5)
        touch((29,82),duration=1)
        sleep(0.5)
        text(users[i])
        sleep(1)
        touch((858,340))
        sleep(0.5)
        touch((29,82),duration=1)
        sleep(0.5)
        text(pwds[i])
        sleep(1)
        touch((787,482))
        sleep(1)
        if exists(Template(r"tpl1734247728595.png", record_pos=(0.107, 0.163), resolution=(1600, 900))):
            touch((993,726)) #可能出现登陆协议
        sleep(3)
def exit_user(i):
    touch_add_button()
    touch(Template(r"tpl1734158766608.png", record_pos=(-0.053, 0.235), resolution=(1600, 900)))
    print("*******SGS_AUTO:"+users[i]+"账号已退出********")
#   1、签到
def sign_in():
    #当天首次登陆，执行签到逻辑
    if match_snapshot(Template(r"tpl1734192122100.png", record_pos=(-0.116, -0.237), resolution=(1600, 900))):
        if not match_snapshot(Template(r"tpl1735106543370.png", record_pos=(-0.304, 0.118), resolution=(1600, 900))):
            sleep(0.5)
            touch((420,720))
        sleep(0.5)
        touch((985,310),times=3)
        sleep(0.5)
        touch((985,525),times=3)
        sleep(0.5)
        touch((985,740),times=3)
        sleep(0.5)
        touch((1550,60)) # 返回键
        sleep(1)
        print("1：T签到成功！")
    else:
        print("1：F无签到弹窗出现，或许您已签到过了！")
#   2、领取每日免费盒子
def get_free():
    for j in range(2):
        touch(Template(r"tpl1737167657879.png", record_pos=(0.144, 0.242), resolution=(1600, 900)))
        sleep(2)
        touch((10,890),times=2)
        sleep(0.5)
        touch(Template(r"tpl1737167723089.png", record_pos=(-0.451, -0.138), resolution=(1600, 900)))
        sleep(0.8)
        libao = match_snapshot(Template(r"tpl1734192384552.png", record_pos=(-0.334, -0.174), resolution=(1600, 900)))
        if libao:
            touch(libao)
            sleep(0.5)
            touch((1400,720),times=2)
            sleep(1)
            touch((1400,520),times=2)
            sleep(1)
            touch((1400,320),times=2)
            sleep(0.5)
            if j==0:
                print("2.1：T领取每日免费礼包盒子成功！")
            else:
                print("2.1：F存在无法领取的免费盒子，不排除等级、时间、版本等问题！")
        else:
            print("2.1：F您已领取过每日免费礼包盒子了！")
        touch_back()
#   2、领取月卡周卡，元宝树
def get_coin():
    pos = match_snapshot(Template(r"tpl1734270140041.png", record_pos=(0.344, -0.235), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(1)
        print("2.2：T开始领取周卡、月卡")
        #周卡
        if not match_snapshot(Template(r"tpl1734439591532.png", threshold=0.85, record_pos=(-0.112, 0.166), resolution=(1600, 900))):

            touch((630,715),times=2)
            print("2.2：T已领取周卡")
            sleep(1.5)
        else:
            print("2.2：F您尚未购买周卡")
        #月卡
        if not match_snapshot(Template(r"tpl1734439631674.png", threshold=0.85, record_pos=(0.284, 0.163), resolution=(1600, 900))):
            touch((1250,715),times=2)
            print("2.2：T已领取月卡")
        else:
            print("2.2：F您尚未购买月卡")
        touch_back()
    else:
        print("2.2：F未找到周卡月卡界面")
    touch(Template(r"tpl1734269712197.png", record_pos=(0.225, 0.259), resolution=(1600, 900)))
    sleep(1)
    touch(Template(r"tpl1734271534528.png", record_pos=(-0.386, -0.029), resolution=(1600, 900)))
    sleep(1)
    
    touch((1330,160))
    sleep(1)
    pos = match_snapshot(Template(r"tpl1734271661315.png", record_pos=(-0.262, -0.149), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(4)
        print("2.3：T开始进行元宝树浇水！")
    else:
        print("2.3：F元宝树已经浇过水了！") 
    while True:
        while True: 
            pos=exists(Template(r"tpl1734272046696.png", record_pos=(-0.071, 0.01), resolution=(1600, 900)))
            if not pos:
                break
            touch(pos,times=3)
            sleep(0.4)
        while True: 
            pos=exists(Template(r"tpl1734272096218.png", record_pos=(0.007, -0.016), resolution=(1600, 900)))
            if not pos:
                break
            touch(pos,times=3)
            sleep(0.4)
        touch((1550,60)) # 返回键
        sleep(1)
        if match_snapshot(Template(r"tpl1734275973399.png", threshold=0.8, record_pos=(-0.096, 0.029), resolution=(1600, 900))):
            print("2.3：F元宝树未完全摘取，正在尝试重新进行")
            touch((950,500))
            sleep(1)
            touch((1330,160))
            sleep(1)
        else:
            print("2.3：T摇树环节结束！")
            break
    touch_back()
#   2、领取周薪（先锋将军以上）
def get_weekly_award():
    pos = match_snapshot(Template(r"tpl1734280447449.png", record_pos=(0.223, 0.259), resolution=(1600, 900)))
    print("2.4：开始领取周薪")
    if pos:
        touch(pos)
        sleep(1.5)
        touch((1350,120),times=2)
        touch((500,120),times=2)
        sleep(0.5)
        touch_back()
#   2、领取邮件
def get_email():
    while True:
        touch_add_button()
        touch(Template(r"tpl1734329690738.png", threshold=0.85, record_pos=(-0.156, 0.161), resolution=(1600, 900)))
        sleep(1)
        flag=0
        pos = match_snapshot(Template(r"tpl1734329727017.png", record_pos=(0.341, 0.196), resolution=(1600, 900)))
        if pos:
            touch((1360,760),times=2)
            print("2.5：T邮件领取成功！")
            sleep(0.5)
        else:
            print("2.5：F所有邮件已经领取成功！")
            flag = 1
        touch((1550,60)) # 返回键
        if flag ==1:
            break
        sleep(1)
#   开一把五人标准场
def color_match(color1, color2, tolerance=5):
    return all(abs(c1 - c2) <= tolerance for c1, c2 in zip(color1, color2))
def start_auto_standard():
    touch_back()
    touch((314,452),times=2) # 进入经典场
    sleep(1)
    while True:
        touch(Template(r"tpl1757140152338.png", record_pos=(-0.307, 0.236), resolution=(1600, 900)))
        sleep(1)
        touch((300,245),times=2)
        sleep(0.3)
        if not match_snapshot(Template(r"tpl1763796508379.png", threshold=0.9, record_pos=(0.205, 0.158), resolution=(1600, 900))):
            touch((1100,700))
            sleep(0.2)
        touch(Template(r"tpl1748012537592.png", record_pos=(0.229, -0.156), resolution=(1600, 900)))
        sleep(1)
        # 演练过快，出现回到演练场的bug，处理一下
        while match_snapshot(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900))):
            touch(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900)))
            sleep(1)
            touch((1163,180))
        sleep(9)
        flag = 0
        cur_screen = G.DEVICE.snapshot()
        if match_snapshot(Template(r"tpl1757216822251.png", threshold=0.85, record_pos=(-0.028, 0.207), resolution=(1600, 900)),cur_screen):
            flag = 1

        touch((380,300),times=2)
        touch((160,617),times=4)
        sleep(2)
        if flag == 1 or match_snapshot(Template(r"tpl1755995838483.png",resolution=(1600, 900))) or match_snapshot(Template(r"tpl1763890384209.png", record_pos=(0.426, -0.106), resolution=(1600, 900))) or match_snapshot(Template(r"tpl1764081124480.png", record_pos=(0.375, 0.129), resolution=(1600, 900))) or match_snapshot(Template(r"tpl1764081162897.png", record_pos=(0.373, 0.129), resolution=(1600, 900))) or match_snapshot(Template(r"tpl1764082382851.png", record_pos=(0.375, 0.129), resolution=(1600, 900))):
            sleep(0.5)
            touch_add_button()
            sleep(0.5)
            touch(Template(r"tpl1743139723798.png", record_pos=(0.383, -0.217), resolution=(1600, 900)))
            sleep(0.5)
            touch((965,560))
            sleep(1.5)
        else:
            ST.CVSTRATEGY = ["tpl"]
            count = 0
            initial_time = time.time()
            while True:
                count+=1
                sleep(0.2)
                cur_screen = G.DEVICE.snapshot()
                pos = match_snapshot((Template(r"tpl1763797504481.png", threshold=0.5, rgb=True, target_pos=5, resolution=(1600, 900))),screen=cur_screen)
                if pos:
                    touch((429,480))
                    touch(pos)
                    continue
                if match_snapshot(Template(r"tpl1734452236121.png", threshold=0.85, resolution=(1600, 900)),screen=cur_screen):
                    touch((180,800))
                    sleep(0.5)
                    touch((740,595))
                    sleep(0.5)
                    continue

                if match_snapshot(Template(r"tpl1763786476012.png", threshold=0.9, rgb=False, record_pos=(0.046, -0.217), resolution=(1600, 900)),screen=cur_screen):
                    touch((800,420),times=2)
                    touch((800,480),times=2)
                    touch((1050,420),times=2)
                    touch((1050,480),times=2)
                    for k in range(10):
                        touch((0+k*150,320),times=2)
                    pos = match_snapshot(Template(r"tpl1764079850578.png", record_pos=(-0.001, 0.152), resolution=(1600, 900)),screen=cur_screen)
                    if pos:
                        touch(pos)
                    continue
                if match_snapshot(Template(r"tpl1734457514682.png", record_pos=(0.094, 0.179), resolution=(1600, 900)),screen=cur_screen):
                    touch((957,784))
                    continue
                if match_snapshot(Template(r"tpl1734453121734.png", threshold=0.92, record_pos=(-0.214, -0.182), resolution=(1600, 900)),screen=cur_screen):

                    sleep(1)
                    touch((42,98),times=2)
                    sleep(2)
                    touch((42,98),times=2)
                    sleep(2)
                    break
                if count % 10 == 0 and ((match_snapshot(Template(r"tpl1738736529990.png", threshold=0.9, record_pos=(0.466, -0.239), resolution=(1600, 900)),screen=cur_screen) or match_snapshot(Template(r"tpl1763881657205.png", record_pos=(-0.011, 0.26), resolution=(1600, 900)),screen=cur_screen))):
                    break
                    
                # 超出15min退出游戏
                if time.time() - initial_time > 900:
                    touch_add_button()
                    sleep(0.5)
                    touch(Template(r"tpl1743139723798.png", record_pos=(0.383, -0.217), resolution=(1600, 900)))
                    sleep(0.5)
                    touch((965,560))
                    sleep(1.5)
                    break
                if color_match(cur_screen[250][225],[131, 125, 78],50):
                    touch((125,368))
                    continue
                elif color_match(cur_screen[70][680],[143, 130, 86],50):
                    touch((580,169))
                    continue
                elif color_match(cur_screen[70][1135],[134, 129, 68],50):
                    touch((1035,166))
                    continue
                elif color_match(cur_screen[250][1592],[138, 120, 59],50):
                    touch((1492,350))
                    continue
                    
                pos = match_snapshot(Template(r"tpl1763798085348.png", threshold=0.7, rgb=True, resolution=(1600, 900)),screen=cur_screen)
                if pos:
                    touch(Template(r"tpl1763798085348.png", resolution=(1600, 900)))
                    continue
                else:
                    touch((1100,660),times=2)
#                 confidence_array = []
#                 confidence_array.append(cal_confidence(Template(r"tpl1763878241141.png", threshold=0.5, rgb=True, record_pos=(0.199, -0.249), resolution=(1600, 900)),screen=cur_screen))
#                 confidence_array.append(cal_confidence(Template(r"tpl1763878222967.png", threshold=0.5, rgb=True, record_pos=(-0.37, -0.143), resolution=(1600, 900)),screen=cur_screen))
#                 confidence_array.append(cal_confidence(Template(r"tpl1763894175821.png", threshold=0.5, rgb=True, record_pos=(0.492, -0.142), resolution=(1600, 900)),screen=cur_screen))
#                 confidence_array.append(cal_confidence(Template(r"tpl1763994899483.png", threshold=0.5, rgb=True, record_pos=(0.49, -0.142), resolution=(1600, 900)),screen=cur_screen))
#                 max_confidence_index =  find_max_index_numpy(confidence_array)
#                 if max_confidence_index == 0:
#                     pos = match_snapshot(Template(r"tpl1763878241141.png", threshold=0.5, rgb=True, record_pos=(0.199, -0.249), resolution=(1600, 900)),screen=cur_screen)
#                     if pos:
#                         x1,y1 = pos
#                         touch((x1-100,y1+100))
#                         continue
#                 elif max_confidence_index == 1:
#                     pos = match_snapshot(Template(r"tpl1763878222967.png", threshold=0.5, rgb=True, record_pos=(-0.37, -0.143), resolution=(1600, 900)),screen=cur_screen)

#                     if pos:
#                         x1,y1 = pos
#                         touch((x1-100,y1+100))
#                         continue
#                 elif max_confidence_index == 2:
#                     pos = match_snapshot(Template(r"tpl1763894175821.png", threshold=0.5, rgb=True, record_pos=(0.492, -0.142), resolution=(1600, 900)),screen=cur_screen)
#                     if pos:
#                         x1,y1 = pos
#                         touch((x1-100,y1+100))
#                         continue
#                 elif max_confidence_index == 3:
#                     pos = match_snapshot(Template(r"tpl1763994899483.png", threshold=0.5, rgb=True, record_pos=(0.49, -0.142), resolution=(1600, 900)),screen=cur_screen)
#                     if pos:
#                         x1,y1 = pos
#                         touch((x1-100,y1+100))
#                         continue
            touch((42,98),times=2)
            sleep(1.5)
            ST.CVSTRATEGY = ["tpl", "sift"]
            break

# 5人标准托管流
def tuoguan():
    touch_back()
    touch((314,452),times=2) # 进入经典场
    sleep(1)
    while True:
        touch(Template(r"tpl1757140152338.png", record_pos=(-0.307, 0.236), resolution=(1600, 900)))
        sleep(1)
        touch((300,245),times=2)
        sleep(0.5)
        touch(Template(r"tpl1748012537592.png", record_pos=(0.229, -0.156), resolution=(1600, 900)))
        sleep(1)
        # 演练过快，出现回到演练场的bug，处理一下
        while exists(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900))):
            touch(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900)))
            sleep(1)
            touch((1163,180))
        num = -1
        sleep(8.5)
        flag = 0
        cur_screen = G.DEVICE.snapshot()
        if match_snapshot(Template(r"tpl1755995998650.png", record_pos=(-0.029, 0.206), resolution=(1600, 900)),cur_screen) or match_snapshot(Template(r"tpl1757216822251.png", threshold=0.85, record_pos=(-0.028, 0.207), resolution=(1600, 900)),cur_screen):
            flag = 1
        touch((480,300),times=2)
        touch((180,617),times=2) 
        sleep(0.5)
        if flag == 1 or match_snapshot(Template(r"tpl1755995838483.png", record_pos=(0.425, 0.134), resolution=(1600, 900))):
            sleep(0.2)
            touch_add_button()
            sleep(0.2)
            touch(Template(r"tpl1743139723798.png", record_pos=(0.383, -0.217), resolution=(1600, 900)))
            sleep(0.4)
            touch((965,560))
            sleep(1.5)
        else:
            while True:
                touch((1070,655),times=2)
                if match_snapshot(Template(r"tpl1734453121734.png", threshold=0.9, record_pos=(-0.214, -0.182), resolution=(1600, 900))):
                    sleep(1)
                    touch((42,98),times=2)
                    sleep(1)
                    touch((42,98),times=2)
                    sleep(1)
                    break
            touch((42,98),times=2)
            sleep(1.5)
            break
            touch_back()
#   开一把五人标准场
def start_standard():
    touch_back()
    touch((314,452),times=2) # 进入经典场
    sleep(1)
    while True:
        touch(Template(r"tpl1757140152338.png", record_pos=(-0.307, 0.236), resolution=(1600, 900)))
        sleep(1)
        touch((300,245),times=2)
        sleep(0.5)
        touch(Template(r"tpl1748012537592.png", record_pos=(0.229, -0.156), resolution=(1600, 900)))
        sleep(1)
        # 演练过快，出现回到演练场的bug，处理一下
        while match_snapshot(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900))):
            touch(Template(r"tpl1734422716170.png", record_pos=(-0.101, 0.044), resolution=(1600, 900)))
            sleep(1)
            touch((1163,180))
        sleep(9)
        flag = 0
        if not match_snapshot(Template(r"tpl1734504834055.png",resolution=(1600, 900))):
            flag = 1
        touch((170,300),times=2)
        touch((160,617),times=2) 
        sleep(0.8)
        if flag == 1 or match_snapshot(Template(r"tpl1755995838483.png",resolution=(1600, 900))):
            sleep(0.5)
            touch_add_button()
            sleep(0.5)
            touch(Template(r"tpl1743139723798.png", record_pos=(0.383, -0.217), resolution=(1600, 900)))
            sleep(0.5)
            touch((965,560))
            sleep(1.5)
        else:
            x,y = exists(Template(r"tpl1734508491650.png", record_pos=(0.481, -0.141), resolution=(1600, 900)))
            count = 0
            while True:
                count+=1
                cur_screen = G.DEVICE.snapshot()
                if match_snapshot(Template(r"tpl1734506769654.png", threshold=0.85, record_pos=(0.176, 0.088), resolution=(1600, 900)),screen=cur_screen):
                    touch((1293,811))
                    sleep(0.2)
                    touch((200,800))
                    touch((350,800))
                    touch((500,800))
                    touch((x-100,y+100))
                    sleep(0.2)
                    touch((750,590))
                    sleep(0.2)
                    touch((1100,593),times=2)
                elif match_snapshot(Template(r"tpl1763790550995.png", record_pos=(0.172, 0.091), resolution=(1600, 900)),screen=cur_screen):
                    touch((1100,593),times=2)
                elif match_snapshot(Template(r"tpl1734453121734.png", threshold=0.92, record_pos=(-0.214, -0.182), resolution=(1600, 900)),screen=cur_screen):
                    sleep(1)
                    touch((42,98),times=2)
                    sleep(1)
                    touch((42,98),times=2)
                    sleep(1)
                    break
                elif match_snapshot(Template(r"tpl1734457514682.png", record_pos=(0.094, 0.179), resolution=(1600, 900)),screen=cur_screen):
                    touch((957,784))
                elif match_snapshot(Template(r"tpl1763786476012.png", threshold=0.9, rgb=False, record_pos=(0.046, -0.217), resolution=(1600, 900)),screen=cur_screen):
                    touch((800,430),times=2)
                    touch((1050,430),times=2)
                    for k in range(10):
                        touch((0+k*150,320),times=2)
                elif match_snapshot(Template(r"tpl1734452236121.png", threshold=0.85, record_pos=(-0.034, 0.088), resolution=(1600, 900)),screen=cur_screen):
                    touch((180,800))
                    sleep(0.5)
                    touch((740,595))
                    sleep(0.5)
                elif match_snapshot(Template(r"tpl1734507394416.png", record_pos=(0.438, -0.072), resolution=(1600, 900)),screen=cur_screen):
                    touch((1559,52))
                    sleep(0.5)
                    touch(Template(r"tpl1743139723798.png", record_pos=(0.383, -0.217), resolution=(1600, 900)))
                    sleep(0.5)
                    touch((965,560))
                    sleep(1.5)
                    break
                # 网络错误导致游戏结束，断掉死循环
                elif count%5==0 and ((match_snapshot(Template(r"tpl1738736529990.png", threshold=0.9, record_pos=(0.466, -0.239), resolution=(1600, 900)),screen=cur_screen) or match_snapshot(Template(r"tpl1763881657205.png", record_pos=(-0.011, 0.26), resolution=(1600, 900)),screen=cur_screen))):
                    break
                else:
                    touch((1100,660),times=2)
            touch((42,98),times=2)
            sleep(1.5)
            break
#   开一把1v1(需要搭配host脚本使用)
def start_1v1():
    touch_back()
    global host_stat
    touch((314,452),times=2) # 进入经典场
    sleep(1)
    touch(Template(r"tpl1741587754628.png", record_pos=(0.001, 0.237), resolution=(1600, 900)))
    sleep(1)
    touch(Template(r"tpl1741587771492.png", record_pos=(-0.305, -0.136), resolution=(1600, 900)))
    sleep(1)
    touch(Template(r"tpl1741587782959.png", record_pos=(0.302, -0.156), resolution=(1600, 900)))
    sleep(1)
    touch((735,360))
    sleep(0.5)
    touch((29,82),duration=1)
    sleep(0.5)
    text(host[0])
    sleep(1)
    touch((735,460))
    sleep(0.5)
    touch((29,82),duration=1)
    sleep(0.5)
    text(host_pwd[0])
    sleep(1)
    touch((930,550))
    sleep(2)
    pos = exists(Template(r"tpl1741588161116.png", record_pos=(-0.003, 0.228), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(4)
        while match_snapshot(Template(r"tpl1741668588732.png", record_pos=(-0.48, -0.128), resolution=(1600, 900))):
            touch((155,526),times=3)
            touch((420,526),times=3)
            touch((670,526),times=3)
            touch((920,526),times=3)
            touch((1170,526),times=3)
            touch((1420,526),times=3)
        sleep(2)
        touch((1138,463),times=2)
        count = 0
        sleep(2)
####################################
#         global xianhua
#         if match_snapshot(Template(r"tpl1745578993465.png", record_pos=(-0.468, 0.241), resolution=(1600, 900))) and xianhua == 1:
#             for i in range(11):
#                 touch(Template(r"tpl1745578993465.png", record_pos=(-0.468, 0.241), resolution=(1600, 900)))
#                 touch((137,729))
#                 sleep(0.2)
#                 touch((785,235))
#                 sleep(1.5)
#             xianhua = 0
###################################
        sleep(1)
        touch((1215,600),times=2)
        while True:
            count+=1
            cur_screen = G.DEVICE.snapshot()
            if (match_snapshot(Template(r"tpl1738736529990.png", record_pos=(0.466, -0.239), threshold=0.85, resolution=(1600, 900)),screen=cur_screen) or match_snapshot(Template(r"tpl1763881657205.png", record_pos=(-0.011, 0.26), resolution=(1600, 900)),screen=local_screen)):
                break
            elif match_snapshot(Template(r"tpl1734506769654.png", threshold=0.85, record_pos=(0.176, 0.088), resolution=(1600, 900)),screen=cur_screen):
                pos = False
                pos = match_snapshot(Template(r"tpl1746019837322.png", threshold=0.9, record_pos=(-0.282, 0.233), resolution=(1600, 900)))
                if pos:
                    touch(pos)
                    sleep(0.1)
                    touch((785,235))
                    sleep(0.1)
                    touch((744,588))
                    sleep(0.1)
                    touch((1100,593))
            elif match_snapshot(Template(r"tpl1734452236121.png", threshold=0.85, record_pos=(-0.034, 0.088), resolution=(1600, 900)),screen=cur_screen):
                touch((480,800))
                touch((650,800))
                touch((800,800))
                sleep(0.2)
                touch((740,595))
            else:
                touch((1100,593))
                touch((1120,784),times=2)
        sleep(0.5)
        touch_back()
        return 1
    else:
        print("3：F找不到1v1主机房间")
        host_stat = 0
        touch_back()
        return -1
#   历练时顺便领取在线礼包
def get_ZaiXianLibao():
    ST.FIND_TIMEOUT_TMP = 1
    pos = match_snapshot(Template(r"tpl1734330797159.png", threshold=0.7, record_pos=(0.466, -0.233), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(1.5)
        touch((1550,60)) # 返回
        sleep(1)
    pos = match_snapshot(Template(r"tpl1734331083959.png", record_pos=(0.466, -0.233), resolution=(1600, 900)))
    if pos:
        print("3：检测到新的邮件，执行2.5领取邮件功能")
        get_email()
    pos = match_snapshot(Template(r"tpl1734330875480.png", threshold=0.7, record_pos=(0.466, -0.234), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(1.5)
        touch((1550,60)) # 返回
        sleep(1)
    # 顺便尝试领个在线礼包
    pos = match_snapshot(Template(r"tpl1737429694389.png", threshold=0.8, record_pos=(0.453, -0.234), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(0.5)
        touch((10,710),times=2)
        sleep(1)
        touch_back()
#   3、跳转至历练页面
def goto_Lilian(index,i):
    touch_back()
    # 领取在线礼包之前排除掉签到、集灯、邮件
    get_ZaiXianLibao()
    touch(Template(r"tpl1737167657879.png", record_pos=(0.144, 0.242), resolution=(1600, 900)))
    sleep(2)
    touch((11,890),times=2)
    sleep(0.5)
    swipe((83,222),(83,751))
    touch(Template(r"tpl1737167723089.png", record_pos=(-0.451, -0.159), resolution=(1600, 900)))
    sleep(1.2)
    EXIT = False
    if not match_snapshot(Template(r"tpl1734154257858.png", record_pos=(-0.339, -0.174), resolution=(1600, 900))):
        print("3：F未找到群英绘壁页面")
        EXIT = True
        touch_back()
        return EXIT
    touch(Template(r"tpl1734154257858.png", record_pos=(-0.339, -0.174), resolution=(1600, 900)))
    sleep(0.5)
    cur_screen = G.DEVICE.snapshot()
    if not match_snapshot(Template(r"tpl1737525267023.png", record_pos=(0.268, 0.156), resolution=(1600, 900)),screen=cur_screen):
        print("3：F未开启历练")
        EXIT = True
    
    if match_snapshot(Template(r"tpl1734158215533.png", threshold=0.95, record_pos=(0.381, 0.091), resolution=(1600, 900)),screen=cur_screen) or match_snapshot(Template(r"tpl1734511399957.png", threshold=0.93, record_pos=(0.271, 0.022), resolution=(1600, 900)),screen=cur_screen):
        if match_snapshot(Template(r"tpl1734511399957.png", threshold=0.93, record_pos=(0.271, 0.022), resolution=(1600, 900)),screen=cur_screen):
            sleep(1)
            print("***3：历练值已满，请更换武将！***")
            touch((1373,485))
        print("3：T历练已完成")
        EXIT = True
    touch_back()  
    return EXIT
#   3、循环历练
def start_Lilian():
    index = 0
    global host_stat,epoch
    print("3：T开始历练")
    while True:
        index+=1
        if host_stat==0:
            # 穿插演武，因为自动演武需要时间战斗，还可能失败，所以尽可能多的尝试，但是可能拖慢整体时间？
            start_auto_standard()
            if sw["7.2 征战天下自动演武"] == True:
                print("3 历练5把后开始尝试自动演武，保证尽可能的用完争霸令！")
                start_YanWu_new()
            if goto_Lilian(index,i) and index >= epoch:
                break
        elif host_stat == 1:
            cd = start_1v1()
            if cd == -1:
                host_stat = 0
                index = index - 1
            if index >= epoch and goto_Lilian(index,i) :
                break
    sleep(1)
#   4、领取每日任务奖励，以及活跃礼包
def daliy_award():
    for i in range(2):
        touch(Template(r"tpl1763881657205.png", record_pos=(-0.011, 0.26), resolution=(1600, 900)))
        sleep(1)
        print("4：开始领取每日活跃任务")
        while True: 
            pos=exists(Template(r"tpl1735015496097.png", rgb=True, record_pos=(0.284, -0.079), resolution=(1600, 900)))

            if not pos:
                break
            touch(Template(r"tpl1735015496097.png", rgb=True, record_pos=(0.284, -0.079), resolution=(1600, 900)))
            touch((27,770),times=2)#点击空白处
            sleep(1)
        pos = exists(Template(r"tpl1734605885296.png", threshold=0.85, target_pos=1, record_pos=(0.093, -0.084), resolution=(1600, 900)))
        if pos:
            x,y = pos
            touch((x+680,y+80))
            sleep(1)
            touch((920,520))
            touch((1200,520))
            touch((920,650))
            touch((1200,650))
            sleep(3)
            touch((920,520))
            touch((1200,520))
            touch((920,650))
            touch((1200,650))
            sleep(2)
        touch((817,125))#领取日活跃宝箱1
        touch((27,770),times=2)#点击空白处
        sleep(0.5)
        touch((1000,125))#领取日活跃宝箱2
        touch((27,770),times=2)#点击空白处
        sleep(0.5)
        touch((1200,125))#领取日活跃宝箱3
        touch((27,770),times=2)#点击空白处
        sleep(0.5)
        touch((1400,125))#领取日活跃宝箱4
        touch((27,770),times=2)#点击空白处
        sleep(0.5)
        touch((807,843))#周活跃宝箱1
        touch((27,770),times=2)
        sleep(0.5)
        touch((1155,843))#周活跃宝箱1
        touch((27,770),times=2)
        sleep(0.5)
        touch_back()        
#   5、领取激活码
def get_code():
    for code in codes:
        touch_add_button()
        touch(Template(r"tpl1734275636492.png", record_pos=(0.053, 0.074), resolution=(1600, 900)))
        touch((700,400))
        touch((29,82),duration=1)
        sleep(0.5)
        text(code)
        sleep(1)
        touch((950,500))
        sleep(0.5)
        if match_snapshot(Template(r"tpl1734275973399.png", record_pos=(-0.096, 0.029), resolution=(1600, 900))):
            print("5：F激活码：“"+code+"”领取失败，可能的原因是过期、已领取、错误等")
            touch((650,500))
            sleep(0.5)
        else:
            print("5：T激活码：“"+code+"”领取成功")
#   6、领取节钺令
def get_JieYueLing():
    touch_back()
    touch(Template(r"tpl1737167657879.png", record_pos=(0.145, 0.261), resolution=(1600, 900)))
    sleep(2)
    swipe((83,751),(83,222))
    pos = match_snapshot(Template(r"tpl1734276851697.png", threshold=0.8, record_pos=(-0.455, 0.042), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(1)
        pos1 = match_snapshot(Template(r"tpl1734276996359.png", threshold=0.8, record_pos=(0.079, 0.231), resolution=(1600, 900)))
        if pos1:
            touch(pos1)
            sleep(3.5)
            touch((1145,883),times=2)
            print("6：T节钺令一键领取成功")
        else:
            print("6：F节钺令已经领取过了")
    else:
        print("6：F未找到节钺令界面")
    sleep(2)
    pos = match_snapshot(Template(r"tpl1735353931082.png", threshold=0.8, record_pos=(-0.453, 0.039), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(1)
        pos1 = match_snapshot(Template(r"tpl1735353951265.png", record_pos=(0.082, 0.231), resolution=(1600, 900)))
        if pos1:
            touch(pos1)
            sleep(3.5)
            touch((1100,800),times=2)
            print("6：T天子令一键领取成功")
        else:
            print("6：F天子令已经领取过了")
        sleep(0.5)
    else:
        print("6：F未找到天子令界面")
    touch_back() 
#   7.2：自动演武5场（三国杀版本4.3.4.1以上）
def start_YanWu_new():
    touch_back()
    touch(Template(r"tpl1734427799591.png", record_pos=(0.314, 0.141), resolution=(1600, 900)))
    sleep(1.8)

    if not match_snapshot(Template(r"tpl1734426588187.png", threshold=0.8, record_pos=(-0.351, 0.253), resolution=(1600, 900))):
        print("7.2：未解锁演武功能")
        touch((1550,60)) # 返回键
        sleep(1)
        return
    touch(Template(r"tpl1734426588187.png", threshold=0.8, record_pos=(-0.351, 0.253), resolution=(1600, 900)))
    print("7.2：开始自动演武")
    sleep(1)
    touch((1413,295),times=4)
    sleep(0.5)
    touch((100,500),times=4)
    touch((761,104),times=2)
    if match_snapshot(Template(r"tpl1737079661027.png", threshold=0.9, record_pos=(0.296, -0.21), resolution=(1600, 900))):
        print("7.2：争霸令已用完！退出演武！")
        touch_back()
        return
    position=[(525,410),(525,580),(525,750),
             (1000,410),(1000,580),(1000,750),
             (1450,410),(1450,580),(1450,750)]
    index_list=[0,1,2,3,4,5,6,7,8]
    random.shuffle(index_list)
    count = 0
    for i in index_list:
        touch(position[i])
        sleep(1)
        a = match_snapshot(Template(r"tpl1741761016472.png", record_pos=(-0.008, -0.141), resolution=(1600, 900)))
        if not a:
            touch((1200,800))
            sleep(1.5)
            if match_snapshot(Template(r"tpl1737077833371.png", record_pos=(0.257, 0.22), resolution=(1600, 900))):
                sleep(0.5)
                touch((1550,60)) # 返回键
                sleep(0.5)
                break
            else:
                count+=1
    print("7.2：本次开启自动演武"+str(count)+"场!")
    touch_back()
#   7.1、征战天下日活
def get_ZhengZhanDaliy():
    touch_back()
    touch(Template(r"tpl1734427799591.png", record_pos=(0.314, 0.141), resolution=(1600, 900)))
    sleep(1)
    pos = match_snapshot(Template(r"tpl1734516652095.png", record_pos=(-0.307, -0.013), resolution=(1600, 900)))
    if pos:
        touch(pos,times=2)
        print("7.1：T领取每日俸禄成功！")
        sleep(3)
        touch((1000,80),times=2)
        sleep(1)
    else:
        print("7.1：F您已领取过每日俸禄了！")
    pos = match_snapshot(Template(r"tpl1734515695607.png", record_pos=(0.37, 0.248), resolution=(1600, 900)))
    if not pos:        
        print("7.1：F未解锁征战天下完整功能，即将退出")
        touch((1550,60)) # 返回键
        sleep(1)
        return
    touch(pos)
    sleep(1)
    print("7.1：尝试进行新版扫荡")
    pos = match_snapshot(Template(r"tpl1751028261028.png", record_pos=(-0.393, 0.22), resolution=(1600, 900)))
    if not pos:
        print("7.1：F未解锁新版扫荡功能")
        sleep(0.5)
    else:
        touch(pos)
        sleep(1)
        touch((335,500))
        sleep(0.2)
        position = [(1250,260),(1250,410),(1250,550),(1250,700)]
        flag = 0
        for i in range(4):
            if flag == 1:
                break
            touch(position[i],times=6)
            sleep(0.5)
            if match_snapshot(Template(r"tpl1734515433749.png", record_pos=(-0.003, -0.138), resolution=(1600, 900))):
                touch((240,156))
                flag = 1
                sleep(1)
                break
        touch((335,600))
        sleep(0.2)
        for i in range(4):
            if flag == 1:
                break
            touch(position[i],times=6)
            sleep(0.5)
            if match_snapshot(Template(r"tpl1734515433749.png", record_pos=(-0.003, -0.138), resolution=(1600, 900))):
                touch((240,156))
                flag = 1
                sleep(1)
                break
        touch((1550,60)) # 返回键
        print("7.1：T扫荡完成！")
        sleep(1.5)
    print("7.1：开始领取征战天下日常和周常奖励")
    while True:
        pos = exists(Template(r"tpl1735015496097.png", rgb=True, record_pos=(0.284, -0.079), resolution=(1600, 900)))
        if not pos:
            break
        sleep(0.5)
        touch(Template(r"tpl1735015496097.png", rgb=True, record_pos=(0.284, -0.079), resolution=(1600, 900)))
        sleep(0.5)
    touch((800,120),times=4)
    sleep(0.5)
    touch((1000,120),times=4)
    sleep(0.5)
    touch((1200,120),times=4)
    sleep(0.5)
    touch((1400,120),times=4)
    sleep(0.5)
    touch((200,280),times=2)
    sleep(1)
    while True:
        pos = exists(Template(r"tpl1735015496097.png", rgb=True, record_pos=(0.284, -0.079), resolution=(1600, 900)))
        if not pos:
            break
        sleep(0.5)
        touch(pos)
        sleep(0.5)
    touch((800,120),times=4)
    sleep(0.5)
    touch((1000,120),times=4)
    sleep(0.5)
    touch((1200,120),times=4)
    sleep(0.5)
    touch((1400,120),times=4)
    sleep(0.5)
    touch((56,70),times=2)
    sleep(0.5)
    touch((1550,60)) # 返回键
    if sw["7.2 征战天下自动演武"] == True:
        pos = match_snapshot(Template(r"tpl1734426588187.png", threshold=0.8, record_pos=(-0.351, 0.253), resolution=(1600, 900)))
        if pos:
            touch(pos)
            sleep(0.5)
            touch((1450,210),times=2)
            touch((80,144),times=2)
            touch((1450,210),times=2)
            touch((80,144),times=2)
            sleep(1)
            touch((1300,210),times=2)
            touch((80,144),times=2)
            touch((1300,210),times=2)
            touch((80,144),times=2)
            sleep(1)
            touch((1160,210),times=2)
            touch((80,144),times=2)
            touch((1160,210),times=2)
            touch((80,144),times=2)
            sleep(0.5)
    touch_back()
#   8：领取家园奖励
def get_home():
    touch_back()
    touch_add_button()
    touch(Template(r"tpl1735285630075.png", record_pos=(0.053, -0.109), resolution=(1600, 900)))
    sleep(2)
    touch((890,100))
    sleep(1)
    touch(Template(r"tpl1735285678537.png", record_pos=(0.174, 0.249), resolution=(1600, 900)))
    sleep(1)
    pos = match_snapshot(Template(r"tpl1735285688155.png", record_pos=(0.347, 0.194), resolution=(1600, 900)))
    if pos:
        touch(pos)
        sleep(2)
        print("8: T每日免费祈福成功")
    else:
        print("8: F已领取每日免费祈福")
    sleep(1)
    touch((1550,60)) # 返回键的位置
    if match_snapshot(Template(r"tpl1736308574325.png", record_pos=(0.256, 0.246), resolution=(1600, 900))):
        position = [(715,240),(715,380),(715,520)]
        for i in range(3):
            touch(Template(r"tpl1736308574325.png", record_pos=(0.256, 0.246), resolution=(1600, 900)))
            sleep(1)
            touch(position[i])
            sleep(1)
            if match_snapshot(Template(r"tpl1736308689074.png", record_pos=(0.256, 0.246), resolution=(1600, 900))):
                touch((1527,820),times=3)
                sleep(0.5)
            if not match_snapshot(Template(r"tpl1736309592535.png", record_pos=(-0.381, -0.228), resolution=(1600, 900))):
                touch((1550,60)) # 返回键的位置
                sleep(1)
            touch((1550,60)) # 返回键的位置
    touch_back()       
#   9：寻味三国奖励
def get_zuofan():
    touch_back()
    touch(Template(r"tpl1737167657879.png", record_pos=(0.145, 0.261), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1748619401352.png", record_pos=(-0.453, -0.048), resolution=(1600, 900)))
    sleep(1)
    swipe((261,130),(261,779))
    swipe((261,130),(261,779))
    swipe((261,130),(261,779))
    sleep(0.5)
    for i in range(4):
        pos = match_snapshot(Template(r"tpl1748615602600.png", record_pos=(-0.338, 0.172), resolution=(1600, 900)))
        sleep(0.5)
        if pos:
            break
        swipe((261,779),(261,51))
        sleep(0.3)
    pos = match_snapshot(Template(r"tpl1748615602600.png", record_pos=(-0.338, 0.172), resolution=(1600, 900)))
    if pos:
        touch(pos,times=2)
        sleep(2)
        touch((1285,672))
        sleep(2)
        touch((1400,810),times=2)
        print("9：开始领取寻味三国日常任务!")
        sleep(3)
        touch((340,468),times=2) # 活动
        touch((1400,810),times=2)
        print("9：开始领取寻味三国活动任务!")
        sleep(3)
        touch((1550,60))
        if sw['10 寻味三国自动兑换一盒一福袋'] == True:
            print("10：您已开启寻味三国自动兑换一盒一福袋！")
            touch((1360,400)) # 珍宝阁
            sleep(1)
            pos = match_snapshot(Template(r"tpl1737179407221.png", record_pos=(0.217, -0.159), resolution=(1600, 900)))
            if pos:
                x,y = pos
                touch((x,y+160))
                sleep(1)
                pos = match_snapshot(Template(r"tpl1737179647956.png", record_pos=(0.122, 0.101), resolution=(1600, 900)))
                if pos:
                    touch(pos)
                    print("10：T灵蛇献瑞礼盒兑换成功！")
                else:
                    print("10：F灵蛇献瑞礼盒兑换失败，失败的原因可能是寻味币不足，已售罄等")
            else:
                print("10：F未找到灵蛇献瑞礼盒")
            sleep(1.5)
            pos = match_snapshot(Template(r"tpl1737179870667.png", record_pos=(0.351, -0.159), resolution=(1600, 900)))
            if pos:
                x,y = pos
                touch((x,y+160))
                sleep(1)
                pos = match_snapshot(Template(r"tpl1737179647956.png", record_pos=(0.122, 0.101), resolution=(1600, 900)))
                if pos:
                    touch(pos)
                    print("10：T蛇年福袋兑换成功！")
                else:
                    print("10：F蛇年福袋兑换失败，失败的原因可能是寻味币不足，已售罄等")
            else:
                print("10：F未找到蛇年福袋")
            sleep(1)
    else:
        print("9：F未找到寻味三国活动")
    touch_back()
def get_linlang():
    touch_back()
    touch(Template(r"tpl1737167657879.png", record_pos=(0.145, 0.261), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1748619401352.png", record_pos=(-0.453, -0.048), resolution=(1600, 900)))
    sleep(1)
    swipe((261,130),(261,779))
    swipe((261,130),(261,779))
    swipe((261,130),(261,779))
    sleep(0.5)
    for i in range(4):
        pos = match_snapshot(Template(r"tpl1748619293683.png", record_pos=(-0.338, 0.105), resolution=(1600, 900)))
        sleep(0.5)
        if pos:
            break
        swipe((261,779),(261,51))
        sleep(0.3)
    if pos:
        touch(pos,times=2)
        sleep(0.5)
        if not match_snapshot(Template(r"tpl1739010944422.png", record_pos=(0.307, 0.209), resolution=(1600, 900))):
            print("11：T已领取琳琅奇境！")
            touch((1287,784),times=2)
            sleep(0.5)
            touch((1287,784),times=2)
        else:
            print("11：F您未开通琳琅奇境！")
    else:
        print("11：F未找到琳琅奇境！")
    touch_back()
def get_XXLe():
    touch_back()
    touch(Template(r"tpl1762525552881.png", record_pos=(0.164, 0.142), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1762525604777.png", record_pos=(-0.116, 0.081), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1762525622947.png", record_pos=(-0.193, 0.089), resolution=(1600, 900)))
    sleep(2)
    touch((184,410),times=8)
    sleep(1)
    touch((360,300),times=2)
    sleep(1)
    board_x = [580,670,760,850,940,1020,1100]
    board_y = [100,190,280,370,460,550,640]
    while True:
        for i in range(6):
            for j in range(6):
                if not match_snapshot(Template(r"tpl1762526588361.png", record_pos=(-0.415, 0.166), resolution=(1600, 900))):
                    break;
                swipe((board_x[i],board_y[j]),(board_x[i+1],board_y[j]),duration=0.5)
                sleep(0.2)
                swipe((board_x[i],board_y[j]),(board_x[i],board_y[j+1]),duration=0.5)
                sleep(0.2)
            if not match_snapshot(Template(r"tpl1762526588361.png", record_pos=(-0.415, 0.166), resolution=(1600, 900))):
                break;
            touch((1446,751),times=2)
        if not match_snapshot(Template(r"tpl1762526588361.png", record_pos=(-0.415, 0.166), resolution=(1600, 900))):
            break;
    touch_back()
        
def get_JGYC():
    touch_back()
    touch(Template(r"tpl1762525552881.png", record_pos=(0.164, 0.142), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1762525604777.png", record_pos=(-0.116, 0.081), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1762532742033.png", record_pos=(0.248, -0.054), resolution=(1600, 900)))
    sleep(1)
    touch(Template(r"tpl1762532760308.png", record_pos=(0.31, 0.0), resolution=(1600, 900)))
    sleep(1)
    try_cnt = 0
    if match_snapshot(Template(r"tpl1762569585656.png", record_pos=(0.224, -0.124), resolution=(1600, 900))):
        sleep(0.5)
        touch((1160,645))
        sleep(0.5)
        touch(Template(r"tpl1762533380551.png", record_pos=(-0.454, -0.219), resolution=(1600, 900)))
    else:
        while True:
            cur_screen = G.DEVICE.snapshot()
            if (match_snapshot(Template(r"tpl1762533311034.png", record_pos=(0.167, 0.002), resolution=(1600, 900)),screen=cur_screen) or match_snapshot(Template(r"tpl1762532760308.png", record_pos=(0.31, 0.0), resolution=(1600, 900)),screen=cur_screen)):
                sleep(0.5)
                touch((1037,490))
                sleep(1)
                touch(Template(r"tpl1762533380551.png", record_pos=(-0.454, -0.219), resolution=(1600, 900)))
                sleep(0.2)
                break
            touch((160,450))
            sleep(0.2)
    touch_back()
def get_ZhuChou():
    touch_back()
    touch(Template(r"tpl1762525552881.png", record_pos=(0.164, 0.142), resolution=(1600, 900)))
    sleep(2)
    touch(Template(r"tpl1762525604777.png", record_pos=(-0.116, 0.081), resolution=(1600, 900)))
    sleep(2)
    touch((970,45))
    sleep(1)
    touch((665,578),times=2)
    sleep(1)
    pos = match_snapshot(Template(r"tpl1762528396145.png", record_pos=(0.102, 0.043), resolution=(1600, 900)))
    if pos:
        touch(pos)
    sleep(1)
    touch((1228,580),times=2)
    sleep(1)
    pos = match_snapshot(Template(r"tpl1762528396145.png", record_pos=(0.102, 0.043), resolution=(1600, 900)))
    if pos:
        touch(pos)
    sleep(1)
    swipe((230,694),(230,177),duration=1)
    touch((665,650),times=2)
    sleep(1)
    pos = match_snapshot(Template(r"tpl1762528396145.png", record_pos=(0.102, 0.043), resolution=(1600, 900)))
    if pos:
        touch(pos)
    sleep(1)
    swipe((230,694),(230,177),duration=1)
    touch((1228,650),times=2)
    sleep(1)
    pos = match_snapshot(Template(r"tpl1762528396145.png", record_pos=(0.102, 0.043), resolution=(1600, 900)))
    if pos:
        touch(pos)
    sleep(1)
    touch((970,45))
    touch_back()
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
def save_config():
    global users,pwds,codes,host,sw,epoch
    users = user_entry.get().split('/')
    pwds = pwd_entry.get().split('/')
    codes = code_entry.get().split('/')
    host = host_entry.get().split('/')
    epoch = int(epoch_entity.get())
    for key in sw:
        sw[key] = check_vars[key].get()
    print("账号:", users)
    print("密码:", pwds)
    print("激活码:",codes)
    print("功能设置:", sw)
    print("至尊1v1房间号:",host)
    print("演练轮数:",epoch)
    root.destroy()
def on_closing():
    root.destroy()  # 关闭窗口
    sys.exit(0)     # 终止程序
root = tk.Tk()
root.title("游戏配置工具")
root.protocol("WM_DELETE_WINDOW", on_closing)
# 添加行提示
tk.Label(root, text="账号密码只有开启自动换号(官服）才需要填写，否则请忽略", 
         fg="red", font=('Arial', 10)).grid(row=0, column=0, columnspan=2, pady=5)

# 账号密码输入框
tk.Label(root, text="账号组(“/”分隔):").grid(row=1, column=0, sticky='w')
user_entry = tk.Entry(root, width=50)
user_entry.grid(row=1, column=1, padx=5, pady=5)
user_entry.insert(0, '/'.join(users))

tk.Label(root, text="密码组(“/”分隔):").grid(row=2, column=0, sticky='w')
pwd_entry = tk.Entry(root, width=50)
pwd_entry.grid(row=2, column=1, padx=5, pady=5)
pwd_entry.insert(0, '/'.join(pwds))

# 功能选择
tk.Label(root, text="功能选择:").grid(row=3, column=0, sticky='nw', pady=5)
frame = tk.Frame(root)
frame.grid(row=3, column=1, sticky='w')

check_vars = {}
for i, (key, value) in enumerate(sw.items()):
    check_vars[key] = tk.BooleanVar(value=value)
    cb = tk.Checkbutton(frame, text=key, variable=check_vars[key])
    cb.grid(row=i, column=0, sticky='w')

# 添加行提示
tk.Label(root, text="开启《领取激活码》功能需要在下面输入激活码，否则请忽略", 
         fg="red", font=('Arial', 10)).grid(row=5, column=0, columnspan=2, pady=5)
# 添加激活码
tk.Label(root, text="激活码(“/”分隔):").grid(row=6, column=0, sticky='w')
code_entry = tk.Entry(root, width=50)
code_entry.grid(row=6, column=1, padx=5, pady=5)
code_entry.insert(0, '/'.join(codes))
# 添加行提示
tk.Label(frame, text="演练轮数（-1表示直到历练值为60；大于0的数表示固定轮数）", 
         fg="red", font=('Arial', 10)).grid(row=4, column=2, columnspan=2, pady=5)
# 演练轮数
tk.Label(frame, text="设置轮数:").grid(row=6, column=1, sticky='nw', pady=5)
epoch_entity = tk.Entry(frame, width=50)
epoch_entity.grid(row=6, column=3, padx=5, pady=5)
epoch_entity.insert(0,str(epoch))
# 保存按钮
# 添加行提示
tk.Label(root, text="如果不想每次都改配置，可在代码30-65行修改，这样就会先加载你的设定", 
         fg="blue", font=('Arial', 10)).grid(row=7, column=0, columnspan=2, pady=5)
save_btn = tk.Button(root, text="三国杀启动！", command=save_config)
save_btn.grid(row=7, column=3, pady=10, sticky='e')
# 添加行提示
tk.Label(root, text="作者测试至尊1v1专用，请直接忽略下面内容", 
         fg="red", font=('Arial', 10)).grid(row=8, column=0, columnspan=2, pady=5)
# 至尊1v1
tk.Label(root, text="至尊1v1房间号:").grid(row=9, column=0, sticky='w')
host_entry = tk.Entry(root, width=50)
host_entry.grid(row=9, column=1, padx=5, pady=5)
host_entry.insert(0, '/'.join(host))

root.mainloop()

#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
#主程序
length = len(users)
if sw["0 自动换号登录（官服）"]==False:
    length = 1
for i in range(length):
    xianhua = 1
    if sw["0 自动换号登录（官服）"]==True:
        print("*******SGS_AUTO:"+users[i]+"尝试登录********")
        #0：官服登陆入口
        login_guanFu(i)
        if exists(Template(r"tpl1734414738442.png", threshold=0.86, record_pos=(0.184, 0.037), resolution=(1600, 900))):
            print("*******SGS_AUTO:"+users[i]+"登陆失败，账号需要验证手机，即将切换其他账号")
            touch(Template(r"tpl1734414836990.png", record_pos=(-0.112, 0.151), resolution=(1600, 900)))
            continue
        elif exists(Template(r"tpl1734245945360.png", threshold=0.85, record_pos=(0.456, -0.236), resolution=(1600, 900))):
            print("*******SGS_AUTO:"+users[i]+"登陆失败，即将切换其他账号，请自行查询原因********")
            touch((45,50))
            continue
    start_time = time.time()
    touch((946,500))
    #1：签到逻辑
    if sw["1 签到"] == True:
        sign_in()
    # 退出狗卡促销广告
    touch((946,500))
    sleep(1)
    touch_back()
    #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    # 下面可以自定义添加
    # 5: 领取激活码
    if sw["5 领取激活码"] == True:
        get_code()
    if sw["7.2 征战天下自动演武"] == True:
        start_YanWu_new()
    # 2.1：领取每日免费礼包的盒子
    if sw["2.1 领取每日免费盒子"] == True:
        get_free()
    # 2.2-2.3：领取周卡月卡，元宝树
    if sw["2.2-2.3 领取月卡周卡，元宝树"]==True:
        get_coin()
    # 2.4：领取周薪
    if sw["2.4 领取周薪"] == True:
        get_weekly_award()
    # 2.5：领取邮件
    if sw["2.5 领取邮件"] == True:
        get_email()
    # 3：循环进行历练，满60或异常则退出
    if sw["3 开始历练"] == True:
        start_Lilian()
    # 4：领取日活奖励
    if sw["4 领取日活、周活"] == True:
        daliy_award()
    # 6: 领取节钺令
    if sw["6 领取节钺令、天子令"] == True:
        get_JieYueLing()
    if sw["7.1 征战天下日活"] == True:
        get_ZhengZhanDaliy()
    # 8：领取家园
    if sw["8 领取家园（请确保事先完成新手引导）"] == True:
        get_home()
    if sw["9 寻味三国任务奖励领取"] == True:
        get_zuofan()
    if sw["11 领取琳琅奇境"] == True:
        get_linlang()
    if sw["12 闲趣百市"] == True:
        for op in range(2):
            get_XXLe()
        for op in range(3):
            get_JGYC()
        get_ZhuChou()
    # 退出时尝试领取在线礼包
    get_ZaiXianLibao()
    # 退出账号
    if sw["0 自动换号登录（官服）"]==True:
        exit_user(i)
    #^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    end_time = time.time()
    run_time = end_time - start_time
    run_time = strftime("%H:%M:%S", gmtime(run_time))
    print('运行时间为：', run_time)
    sleep(5)








    
    
    



















